/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * leafget
 * Copyright (C) xinliGG 2008 <xinliGG@gmail.com>
 * 
 * leafget is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * leafget is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _MAINWINDOW_H_
#define _MAINWINDOW_H_

#include <gtkmm/actiongroup.h>
#include <gtkmm/box.h>
#include <gtkmm/cellrendererprogress.h>
#include <gtkmm/label.h>
#include <gtkmm/liststore.h>
#include <gtkmm/menubar.h>
#include <gtkmm/paned.h>
#include <gtkmm/scrolledwindow.h>
#include <gtkmm/statusbar.h>
#include <gtkmm/statusicon.h>
#include <gtkmm/toggleaction.h>
#include <gtkmm/treeselection.h>
#include <gtkmm/treestore.h>
#include <gtkmm/treeview.h>
#include <gtkmm/uimanager.h>
#include <gtkmm/window.h>

#include <vector>

#include "leafget.h"
#include "leafget_store.h"
#include "left_model_tree.h"
#include "right_list_model.h"
#include "net.h"
 
#define MAX_TASKS 10

class main_window: public Gtk::Window 
{
public:

	main_window ( char ** );
	virtual ~main_window ( void );

		/* open cmd links */

	void download_link ( const std::string & );

		/* set functions */

	const bool set_default ( const bool & );

private:

		/* build functions */
		
	void build_menu_bar    ( void );
	void build_left_tree  ( void );
	void build_right_list  ( void );
	void build_status_bar  ( void );
	void build_status_icon ( void );
	void build_right_msg_window (void);
	
		/* load functions */

	void load_config  ( void );
	void load_history ( void );
		
		/* save functions */
		
	void save_config  ( void );
	void save_history ( void );

		/* open cmd link */

	bool on_cmd_link  ( void );
	
		/* menubar events */

	virtual void on_menu_file_quit            ( void );
	virtual void on_menu_view_status          ( void );
	virtual void on_menu_view_preferences     ( void );
	virtual void on_menu_task_new             ( void );
	virtual void on_menu_task_resume          ( void );
	virtual void on_menu_task_pause           ( void );
	virtual void on_menu_task_resume_all      ( void );
	virtual void on_menu_task_pause_all       ( void );
	virtual void on_menu_task_up              ( void );
	virtual void on_menu_task_down            ( void );
	virtual void on_menu_task_remove          ( void );
	virtual void on_menu_help_about           ( void );
	
		/* group panel events */
		
	virtual void on_treeview_cursor_changed   ( void );

		/* status icon events */

	virtual void on_status_icon_activate      ( void );

		/* paused - completed - failed */

	virtual void on_download_paused    ( Net * );
	virtual void on_download_completed ( Net * );
	virtual void on_download_failed    ( Net * );

		/* action functions */

	void  define_home                  ( void );

	void  start                        ( LEAF_TASK_DATA * );
	void  refresh_downloads            ( int );

	void  connect_list_timer           ( void );
	void  disconnect_list_timer        ( void );

		/* list timer event */

	bool   on_list_timer      ( void );

		
		/* data */

	bool update_list;
		
	LEAF_CONFIG_DATA config;

	LEAF_Store < LEAF_TASK_DATA >      downloadsStore;
	LEAF_Store < LEAF_COMPLETED_DATA > completedsStore;
	LEAF_Store < LEAF_TRASH_DATA >    failedsStore;
	LEAF_Store < LEAF_TASK_DATA >      queueStore;

		/* tasks threads */

	Net	*thread[MAX_TASKS];
	int     threadCount;

		/* widgets */

	Gtk::VBox   mainBox;
	
	Gtk::HPaned HPaned;
	Gtk::VPaned VPanedR;
	
		/* menubar and toolbar */

	Glib::RefPtr< Gtk::ActionGroup >  refActionGroup;
	Glib::RefPtr< Gtk::UIManager >    refUIManager;
	Glib::RefPtr< Gtk::ToggleAction > refStatusToggle;
	
		/* tree panel - left */

	Leaf_model_tree                    tableModel_L;
	Gtk::ScrolledWindow                ScrolledWindow_L;
	Gtk::TreeView                      TreeView_L;
	Glib::RefPtr< Gtk::TreeStore >     refTreeModel_L;
	Glib::RefPtr< Gtk::TreeSelection > refTreeSelection_L;
	
		/* list panel - right */

	Right_list_model                   Columns_R1;
	Gtk::ScrolledWindow                ScrolledWindow_R1;
	Gtk::TreeView                      TreeView_R1;
	Glib::RefPtr< Gtk::ListStore >     refTreeModel_R1;
	Glib::RefPtr< Gtk::TreeSelection > refTreeSelection_R1;
	sigc::connection                   list_timer;

//	int listGroup;
//	CURRENT_STATE current_state;
	enum { RUNNING_GROUP , COMPLETED_GROUP , TRASH_GROUP } listGroup;
			
		/* status bar */

	Gtk::Statusbar   statusBar;
	Gtk::Label       speedStatus;
	sigc::connection statusBar_timer;
	
		/* xpm icon */

	Glib::RefPtr< Gdk::Pixbuf >      refPixbuf_winIcon;
	
		/* system tray icon */

	Glib::RefPtr <Gtk::StatusIcon >  status_icon;

		/* open cmd */

	bool               is_cmd;
	bool               is_default;
	std::string        link;
	sigc::connection   cmd_connect;
	char             **argv;

		/* local information */
	std::string        homePath;
};

#endif // _MAINWINDOW_H_
